// merchant//merchants/transit/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      let tagindex = options.type;
      wx.showLoading({
        title: '加载中',
      })
      if (tagindex == 1) {//玖玖卡详情、适用商家
        this.toJiucard();
      }
      if (tagindex == 2) {
        this.toCoupon();
      }
    }
  },

  toJiucard: function () {
    let that = this;
    if (app.globalData.userInfo) {
      wx.request({
        url: app.globalData.host + '/api/Coupon/GetCardItemListByUser?createUid=0&uid=' + app.globalData.userInfo.Uid,
        success: function (res) {
          wx.hideLoading();
          if (res.data.Code == 0) {
            if (res.data.Data.List.length > 0) {
              let list = res.data.Data.List;
              for (let i = 0; i < list.length; i++) {
                if (list[i].CardType == 4) {
                  wx.redirectTo({
                    url: '../jiuCardDetail/index?cardType=4&cid=' + list[i].Cid,
                  })
                  break;
                }
              }
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
          }
        }
      })
    } else {
      setTimeout(function () {
        this.toJiucard();
      }.bind(that), 1000)

    }
  },
  toCoupon: function () {
    let that = this;
    wx.hideLoading();
    if (app.globalData.userInfo) {
      let cardId = wx.getStorageSync('cardId');
      if (!cardId) {
        wx.request({
          url: app.globalData.host + '/api/BusinessCard/GetAiCardListByJJK?uid=' + app.globalData.userInfo.Uid + '&page=1&pageSize=1',
          success: function (res) {
            if (res.data.Code == 0 && res.data.Data) {
              let cardList = res.data.Data.staff;
              wx.setStorageSync('cardId', cardList[0].Id);
            }
          },
        })
      }
      wx.reLaunch({
        url: '../myCardCoupon/index?tagindex=2&fromwxcard=1',
      })
    } else {
      setTimeout(function () {
        this.toCoupon();
      }.bind(that), 1000)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})